export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

function http(url:string,success:Function){
  wx.request({
    url,
    header:{"content-type":"application/json"},
    method:"GET",
    responseType:"text",
    success:res=>{
      success(res)
    }
  })
}

function handleStar(num:number){
  var res = [];
  for(var i = 0 ; i < 5 ; i++){
    if(num>=2){
        res.push(2);
    }else if(num>0){
        res.push(parseFloat(num.toFixed(1)));
    }else{
        res.push(0);
    }
    num -= 2;
  }
  return res;
}

function handleStr(str:string){
  if(str.length>6){
    str = str.slice(0,6) + "...";
  }
  return str;
}

module.exports = {
  formatTime,
  http,
  handleStar,
  handleStr
}